package com.tesserate.pmt;

import java.text.FieldPosition;
import java.text.Format;
import java.text.MessageFormat;
import java.text.ParsePosition;

public class TimeFormat extends Format {
	private static final long serialVersionUID = -2341737400799741947L;
	MessageFormat mf = new MessageFormat("{0, number,##0}:{1, number,#00}:{2, number,#00}");
	
	@Override
	public StringBuffer format(Object number, StringBuffer toAppendTo, FieldPosition pos) {
		if (number instanceof Double ) {
			double value = Double.valueOf(number.toString());
			int hora = 0;
			int minuto = 0;
			int segundo = 0;
			
			hora = (int)(value /(60*60*1000));
			minuto = (int)((value - hora*60*60*1000) / (60*1000));
			segundo = (int)((value - (hora*60*60*1000) - (minuto * 60*1000)) / (1000));	
			Object[] values = {hora, minuto, segundo};
			toAppendTo.append(mf.format(values));
			return toAppendTo;
		} else {
			throw new IllegalArgumentException("Cannot format given Object as a TimeFormat");
		}
		
	}

	@Override
	public Object parseObject(String source, ParsePosition pos) {
		// TODO Auto-generated method stub
		return null;
	}

}
